<?php

namespace Core\Session;

use Zend\Authentication\Storage\Session;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class AuthStorage extends Session
    implements ServiceLocatorAwareInterface{

    protected $serviceLocator;
    protected $namespace;

    public function __construct($namespace = null)
    {
        parent::__construct($namespace);

        $this->namespace = $namespace;
        $this->member = 'user';

    }

    public function write($contents)
    {
        parent::write($contents);
        /**
        when $this->authService->authenticate(); is valid, the session
        automatically called write('username')
        in this case, i want to save data like
        ["storage"] => array(4) {
        ["id"] => string(1) "1"
        ["username"] => string(5) "admin"
        ["ip_address"] => string(9) "127.0.0.1"
        ["user_agent"] => string(81) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7;
        rv:18.0)
        Gecko/20100101    Firefox/18.0"
        }*/
       /* if (is_array($contents) && !empty($contents)) {
            $this->getSessionManager()
                ->getSaveHandler()
                ->write($this->getSessionId(), \Zend\Json\Json::encode($contents));
        }*/
    }

    public function clear()
    {
        $this->getSessionManager()->getSaveHandler()->destroy($this->getSessionId());
        parent::clear();
    }


    public function getSessionManager()
    {
        return $this->session->getManager();
    }

    public function getSessionId()
    {
        return $this->session->getManager()->getId();
    }



    /**
     * Set service locator
     *
     * @param ServiceLocatorInterface $serviceLocator
     */
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }

    /**
     * Get service locator
     *
     * @return ServiceLocatorInterface
     */
    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }
}